package tianwang.test;

import java.io.File;
import java.io.IOException;

import tianwang.dao.IBatisDao;
import tianwang.model.CAFile;
import tianwang.model.CASentence;
import tianwang.model.CASentenceFile;
import tianwang.util.FileUtil;
import tianwang.utils.punctuator.PunctuatorStanfordImpl;
/**
 * 对原始文件进行解析，
 * 并记录到相应的数据库中
 */
public class AnalysisOriginCopos {
	
	private IBatisDao dao = null;
	private PunctuatorStanfordImpl pun = null;
	public AnalysisOriginCopos() throws IOException{
		dao =new IBatisDao();
		this.pun = new PunctuatorStanfordImpl();
	}
	
	private String basePath = "D:\\测试语料\\";
	public static void main(String[] a) throws Exception{
		AnalysisOriginCopos main = new AnalysisOriginCopos();
		
		File folderFiles = new File(main.basePath);
		
		if(folderFiles.isDirectory()){
			File[] files = folderFiles.listFiles();
			
			for(int f = 0, fLen = files.length; f< fLen; f++){
				main.dealEachFile(files[f]);
			}
			
			System.out.println("计算完成！");
		}
		else{
			System.err.println("in文件夹可能不存在！");
		}
		
	
		
		
	}
	
	public void dealEachFile(File file) throws Exception{
		if(file.isDirectory()){
			File[] subfiles = file.listFiles();
			for(File subFile :subfiles){
				dealEachFile(subFile);
			}
		}
		

		if(file.isDirectory()){
			return;
		}
		
		CAFile fileModel = new CAFile();
		fileModel.setPath(file.getAbsolutePath().replace(this.basePath, ""));
		Integer fileId = this.dao.insertFile(fileModel);
		
		System.out.println("正在处理" +file.getName());
		
		String content = FileUtil.getContent(file.getAbsolutePath());
		String[] sents = this.pun.punctuate(content);
		
		for(String str: sents){
		
			CASentence sentenceModel = new CASentence();
			sentenceModel.setContent(str);
			Integer sentenceId = this.dao.insertSentence(sentenceModel);
			
			CASentenceFile sentFileModel = new CASentenceFile();
			sentFileModel.setFile_id(fileId);
			sentFileModel.setSentc_id(sentenceId);
			
			this.dao.insertSentenceFile(sentFileModel);
			
		}
		
//		
//			TypedDependenciesStanford tds = new TypedDependenciesStanford();
//			Collection<TypedDependency> tdCollection = tds.typedDependencies(str);
//			IBatisDao dao = new IBatisDao();
//			for(TypedDependency td : tdCollection){
//				TDModel tdModel = new TDModel(td.toString());
//				tdModel.setFrom(file.getName());
//				tdModel.setContext(str);
//				dao.insert(tdModel);
//			}
//		}
		
	}
		
}
